//@author A0096466N
//
// **********
//
// **********

#include "Task.h"

Task::Task(void){
	_status = WIP;
	_dateInSeconds = NIL;
	_taskName = EMPTY;
	_deadline = EMPTY;
	_priority = LOW;
	_time = EMPTY;
	
	_modifiedTaskName = EMPTY;
	_modifiedDeadline = EMPTY;
	_modifiedPriority = EMPTY;
	_modifiedTime = EMPTY;
	_modifiedDateInSeconds = NIL;
	_taskLine = NIL;
	_modifiedStatus = EMPTY;
}

Task::~Task(void) {

}

void Task::changeTaskName(string taskName) {
	_taskName = taskName;
	return;
}
	
void Task::changeDeadline(string deadline) {
	_deadline = deadline;
	return;
}

void Task::changePriority(string priority) {
	_priority = priority;
	return;
}

void Task::changeTaskLine(int lineNo) {
	_taskLine = lineNo;
	return;
}

void Task::changeTime(string time) {
	_time = time;
	return;
}

void Task::changeModifiedTaskName(string taskName){
	_modifiedTaskName = taskName;
	return;
}

void Task::changeModifiedDeadline(string deadline) {
	_modifiedDeadline = deadline;
	return;
}
	
void Task::changeModifiedPriority(string priority) {
	_modifiedPriority = priority;
	return;
}

void Task::changeModifiedTime(string time) {
	_modifiedTime = time;
	return;
}

void Task::changeStatus(string status) {
	_status = status;
	return;
}

void Task::changeModifiedStatus(string status) {
	_modifiedStatus = status;
	return;
}

void Task::changeDateInSeconds() {
	if(_deadline == EMPTY) {
		_dateInSeconds = NIL;
		return;
	}
	
	/* current time in seconds */
	time_t rawtime;
    time(&rawtime);  
	
	struct tm * currentTime;
    currentTime = localtime(&rawtime);
 
	 // making current time point to today at 00:00
    (*currentTime).tm_hour = 0;                            
    (*currentTime).tm_min = 0;
    (*currentTime).tm_sec = 0;
    time_t today = mktime(currentTime);

	istringstream read(_deadline);
    int day;
    int month;
	int year = NIL;
    char dummy;
		
    read >> day;
    read >> dummy;
    read >> month;
	read >> dummy;
	read >> year;
    month -= 1;

	if(year != NIL) {
		if(year < 2000) {
			year = year + 2000 - 1900;
		} else {
			year = year -1900;
		}
	}
	
	struct tm * deadlineTime;
	deadlineTime = currentTime;
	(*deadlineTime).tm_mon = month; 
	(*deadlineTime).tm_mday = day;
	
	// year is stated by user
	if(year != NIL) {									
		(*deadlineTime).tm_year = year;					
		time_t deadline = mktime(deadlineTime);			
		if(deadline < 0) {
			throw exception("deadline error");
		}
		_dateInSeconds = deadline;							
	
	} else {
	
		time_t deadline = mktime(deadlineTime);
		if(deadline < 0) {
			throw exception("deadline error");
		}
		// if deadline entered is after today, do nothing.
		if(deadline >= today) {
			_dateInSeconds = deadline;						

		// else, push it to next year time.
		} else {
			(*deadlineTime).tm_year += 1;					
			deadline = mktime(deadlineTime);
			_dateInSeconds = deadline;
		}
	}

	return;
}

void Task::changeDateInSeconds(int seconds) {
	_dateInSeconds = seconds;
}

void Task::changeModifiedDateInSeconds() {
	if(_modifiedDeadline == EMPTY) {
		_modifiedDateInSeconds = NIL;
		return;
	}

	/* current time in seconds */
	time_t rawtime;
    time(&rawtime);  
		
	struct tm * currentTime;
    currentTime = localtime(&rawtime);
 
	 // making current time point to today at 00:00
    (*currentTime).tm_hour = 0;                            
    (*currentTime).tm_min = 0;
    (*currentTime).tm_sec = 0;
    time_t today = mktime(currentTime);

	istringstream read(_modifiedDeadline);
    int day;
    int month;
	int year = NIL;
    char dummy;
		
    read >> day;
    read >> dummy;
    read >> month;
	read >> dummy;
	read >> year;
    month -= 1;
	
	
	if(year != NIL) {
		if(year < 2000) {
			year = year + 2000 - 1900;
		} else {
			year = year -1900;
		}
	}

	struct tm * deadlineTime;
	deadlineTime = currentTime;
	(*deadlineTime).tm_mon = month; 
	(*deadlineTime).tm_mday = day;
    
	// year is stated by user
	if(year != NIL) {									
		(*deadlineTime).tm_year = year;					
		time_t deadline = mktime(deadlineTime);			
		_modifiedDateInSeconds = deadline;						
		
	} else {
	
		time_t deadline = mktime(deadlineTime);
		
		// if deadline entered is after today, do nothing special
		if(deadline >= today) {
			_modifiedDateInSeconds = deadline;					

		// else, push it to next year time.
		} else {
			(*deadlineTime).tm_year += 1;					
			deadline = mktime(deadlineTime);
			_modifiedDateInSeconds = deadline;
		}
	}
		
	return;
}

void Task::changeModifiedDateInSeconds(int seconds) {
	_modifiedDateInSeconds = seconds;
}
string Task::getTaskName() {
	return _taskName;
}

string Task::getDeadline() {
	return _deadline;
}

string Task::getPriority() {
	return _priority;
}

int Task::getTaskLine() {
	return _taskLine;
}

string Task :: getTime(){
	return _time;
}

string Task::getModifiedTaskName() {
	return _modifiedTaskName;
}

string Task::getModifiedDeadline() {
	return _modifiedDeadline;
}

string Task::getModifiedPriority() {
	return _modifiedPriority;
}

string Task::getModifiedTime() {
	return _modifiedTime;
}

string Task::getStatus() {
	return _status;
}

string Task::getModifiedStatus() {
	return _modifiedStatus;
}

time_t Task::getDateInSeconds() {
	return _dateInSeconds;
}

time_t Task::getModifiedDateInSeconds() {
	return _modifiedDateInSeconds;
}